// pages/bloodsugarfile/bloodsugarfile.js
import Message from 'tdesign-miniprogram/message/index';

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileName: "选择文件",
        filePath: "",
        sid: "",
        valueColName: "",
        timeColName: "",
        hideChooseCol: true,


        bloodsugarColumn: {
            current: 1,
            options: [],
        },
        timeColumn: {
            current: 0,
            options: [],
        },

        showDialog: true,

    },
    valueOnChange(event) {
        console.log(event)
        var that = this;
        var index = event.detail.index
        that.setData({
            ['bloodsugarColumn.current']: index,
            valueColName: that.data.bloodsugarColumn.options[index].label
        })
    },
    timeOnChange(event) {
        console.log(event)
        var that = this;
        var index = event.detail.index
        that.setData({
            ['timeColumn.current']: index,
            timeColName: that.data.timeColumn.options[index].label
        })
    },
    closeDialog(e) {
        this.setData({
            showDialog: false
        })
    },
    uploadClick() {
        var that = this;
        wx.showLoading({
            title: '上传中',
        });
        console.log(that.data.sid)
        console.log(that.data.timeColName)
        console.log(that.data.valueColName)
        wx.request({
            url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/user?Action=LoadData`,
            method: 'POST',
            header: {
                "Content-Type": "application/json",
                "x-api-key": wx.getStorageSync('token')
            },
            data: {
                "sid": that.data.sid,
                 "time": that.data.timeColName,
                 "value": that.data.valueColName,
            },
            success(res) {
                if (res.statusCode == 200) {
                    console.log(res)
                    Message.success({
                        context: this,
                        offset: [0, 0],
                        duration: 2000,
                        content: '数据上传成功',
                    });
                    // setTimeout(function () {
                    //     wx.switchTab({
                    //         //注意要回到根目录，pages前加/
                    //         url: '/pages/bloodsugar/bloodsugar',
                    //     });
                    // }, 2000)

                } else {
                    Message.error({
                        context: this,
                        offset: [0, 0],
                        duration: 2500,
                        content: '数据上传失败',
                    });
                }
            },
            complete() {
                wx.hideLoading();
            }
        })
    },
    chooseFile() {

        //使得wx函数内可以对data赋值
        var that = this;
        //选择文件
        wx.chooseMessageFile({
            count: 1, // 最多可以选择1个文件
            type: 'file', // 可选择的文件类型，这里选择任意文件类型
            success(res) {
                // console.log(res);                    
                //显示文件名
                that.setData({
                    fileName: res.tempFiles[0].name,
                    filePath: res.tempFiles[0].path
                })
                wx.showLoading({
                    title: '上传中',
                });
                //上传文件
                wx.uploadFile({
                    filePath: that.data.filePath,
                    name: 'file',
                    url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/files/upload?Name=avatar`,
                    method: 'POST',
                    header: {
                        "Content-Type": "application/json",
                        "x-api-key": wx.getStorageSync('token')
                    },
                    success(res) {
                        //文件上传成功
                        if (res.statusCode == 200) {
                            var data = JSON.parse(res.data);
                            that.setData({
                                sid: data.message
                            })
                            console.log(that.data.sid);
                            //获取文件列名
                            wx.request({
                                url: `${app.globalData.protocol}://${app.globalData.host}/bgmp/api/user?Action=SelectCols`,
                                method: 'POST',
                                header: {
                                    "Content-Type": "application/json",
                                    "x-api-key": wx.getStorageSync('token')
                                },
                                data: {
                                    "sid": that.data.sid
                                },
                                success(res) {
                                    //获取列名成功
                                    if (res.statusCode == 200) {
                                        var message = res.data.message;
                                        var options = [];
                                        for (var i = 0; i < message.length; i++) {
                                            options.push({
                                                value: i,
                                                label: message[i]
                                            });
                                        }
                                        that.setData({
                                            hideChooseCol: false,
                                            ['bloodsugarColumn.options']: options,
                                            ['timeColumn.options']: options,
                                            valueColName: options[1].label,
                                            timeColName: options[0].label
                                        })

                                    } else {
                                        console.log("文件解析错误", res)
                                        Message.error({
                                            context: this,
                                            offset: [0, 0],
                                            duration: 2500,
                                            content: '文件解析错误',
                                        });
                                    }
                                },
                                complete() {
                                    wx.hideLoading();
                                }
                            })
                        } else {
                            console.log("选择文件上传错误", res)
                            Message.error({
                                context: this,
                                offset: [0, 0],
                                duration: 2500,
                                content: '文件上传错误',
                            });
                        }
                    },
                    fail(res) {
                        console.log("服务器错误", res)
                        Message.error({
                            context: this,
                            offset: [0, 0],
                            duration: 2500,
                            content: '服务器故障，我们将尽快维修',
                        });
                    },
                    complete() {
                        wx.hideLoading();
                    }
                })
            },
            fail(res) {
                console.log("选择文件出现错误", res)
                Message.error({
                    context: this,
                    offset: [0, 0],
                    duration: 2500,
                    content: '当前无法选择文件，我们将尽快维修',
                });
            }
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})